package org.zht.dormpro.dorm.controller;

import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.bean.StudentDTO;
import org.zht.dormpro.dorm.controller.base.AbsBaseController;
import org.zht.dormpro.dorm.controller.base.BaseControllerCallback;
import org.zht.dormpro.dorm.model.*;
import org.zht.dormpro.dorm.service.*;
import org.zht.dormpro.pub.ResponseMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dorm/student")
public class StudentController extends AbsBaseController {
    @Autowired
    private IStudentService studentService;
    @Autowired
    private ICollegeService collegeService;
    @Autowired
    private IMajorService majorService;
    @Autowired
    private IClassService classService;
    @Autowired
    private IFloorService floorService;
    @Autowired
    private IDormService dormService;

    @RequestMapping("addStudentPage")
    public String addStudentPage(Model model) {
        //后台直接放在域中带过去,没必要ajax
        List<TbCollege> allCollege = collegeService.getAllCollege();
        List<TbFloor> floors = floorService.getAllFloor();
        model.addAttribute("colleges", allCollege);
        model.addAttribute("floors", floors);
        return "student/student_add";
    }

    @RequestMapping("studentPage")
    public String studentPage(Model model) {
        //后台直接放在域中带过去,没必要ajax
        List<TbCollege> allCollege = collegeService.getAllCollege();
        List<TbFloor> floors = floorService.getAllFloor();
        model.addAttribute("colleges", allCollege);
        model.addAttribute("floors", floors);
        return "student/student_list";
    }

    @PostMapping("addStudent")
    @ResponseBody
    public ResponseMap addStudent(@RequestParam Map<String, Object> map) {
        final TbStudent tbStudent = studentService.getStudentFromMap(map);
        //业务逻辑返回
        return super.handle(new BaseControllerCallback() {
            @Override
            public boolean handle() throws Exception {
                boolean b = studentService.addStudent(tbStudent);
                return b;
            }
        }, "学生信息添加", true);
    }

    @RequestMapping("deleteStudent/{id}")
    @ResponseBody
    public ResponseMap deleteStudent(@PathVariable("id") final Integer id) {
        //业务逻辑返回
        return super.handle(new BaseControllerCallback() {
            @Override
            public boolean handle() throws Exception {
                boolean b = studentService.deleteStudent(id);
                return b;
            }
        }, "学生信息删除", true);
    }

    @RequestMapping("editStudent")
    @ResponseBody
    public ResponseMap editStudent(@RequestParam Map<String, Object> map) {
        final TbStudent tbStudent = studentService.getStudentFromMap(map);
        //业务逻辑返回
        return super.handle(new BaseControllerCallback() {
            @Override
            public boolean handle() throws Exception {
                boolean b = studentService.updateStudent(tbStudent);
                return b;
            }
        }, "学生信息更新", true);
    }

    @ResponseBody
    @RequestMapping("listStudent")
    public ResponseMap listStduent(@RequestParam Map<String, Object> map) {
        PageBean pageBean = new PageBean(map);
        List<StudentDTO> studentDTOS = studentService.getStudentListByCondition(pageBean);
        return ResponseMap.success(studentDTOS, super.setTotal(pageBean));
    }

    @RequestMapping("getMajByCol/{colId}")
    @ResponseBody
    public ResponseMap getMajByCol(@PathVariable("colId") String colId) {
        List<HashMap<String, Object>> list = new ArrayList<>();
        List<TbMajor> majorByCollegeId = majorService.getMajorByCollegeId(colId);
        //这里这样写是因为前台抽取了方法规定了下统一格式
        if (CollectionUtil.isNotEmpty(majorByCollegeId)) {
            HashMap<String, Object> temp = null;
            for (TbMajor tbMajor : majorByCollegeId) {
                temp = new HashMap<>();
                temp.put("id", tbMajor.getId());
                temp.put("name", tbMajor.getMajName());
                list.add(temp);
            }
        }
        return ResponseMap.success(list);
    }


    @RequestMapping("getClaByMaj/{majId}")
    @ResponseBody
    public ResponseMap getClaByMaj(@PathVariable("majId") String majId) {
        List<HashMap<String, Object>> list = new ArrayList<>();
        List<TbClass> classByMajId = classService.getClassByMajId(majId);
        //这里这样写是因为前台抽取了方法规定了下统一格式
        if (CollectionUtil.isNotEmpty(classByMajId)) {
            HashMap<String, Object> temp = null;
            for (TbClass tbClass : classByMajId) {
                temp = new HashMap<>();
                temp.put("id", tbClass.getId());
                temp.put("name", tbClass.getClaName());
                list.add(temp);
            }
        }
        return ResponseMap.success(list);
    }

    @RequestMapping("getDormByFloor/{floorId}")
    @ResponseBody
    public ResponseMap getDormByFloor(@PathVariable("floorId") String floorId) {
        List<TbDorm> dorms = dormService.getDormByFloorId(floorId);
        List<HashMap<String, Object>> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dorms)) {
            HashMap<String, Object> temp = null;
            for (TbDorm tbDorm : dorms) {
                temp = new HashMap<>();
                temp.put("id", tbDorm.getId());
                temp.put("name", tbDorm.getDormName());
                list.add(temp);
            }
        }
        return ResponseMap.success(list);
    }

    @RequestMapping("getBedByDorm/{dormId}")
    @ResponseBody
    public ResponseMap getBedByDorm(@PathVariable("dormId") String dormId) {
        //获取宿舍还剩下几个床铺信息
        //使用redis进行控制
        //一个宿舍初始是6个床铺
        List<HashMap<String, Object>> allBed = dormService.getAllBed(dormId);
        return ResponseMap.success(allBed);
    }
}
